package cn.gok.oa.system.service.impl;

import cn.gok.oa.system.dto.UserDto;
import cn.gok.oa.system.entity.*;
import cn.gok.oa.system.vo.MenuVo;
import cn.gok.oa.system.vo.UserEmailList;
import cn.gok.oa.system.vo.UserListVo;
import cn.gok.oa.system.vo.UserVo;
import cn.gok.oa.system.mapper.*;
import cn.gok.oa.system.service.ITbUserService;
import cn.gok.oa.util.DateUtil;
import cn.gok.oa.util.JsonUtil;
import cn.gok.oa.util.MD5Util;
import cn.gok.oa.util.TokenUtils;
import cn.hutool.core.date.DateUnit;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gok
 * @since 2022-08-04
 */
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser> implements ITbUserService {
    @Autowired
    TbUserMapper userMapper;
    @Autowired
    TbDeptMapper deptMapper;
    @Autowired
    TbUserDetailMapper userDetailMapper;
    @Autowired
    TbMenuMapper menuMapper;

    @Autowired
    TbRoleMapper roleMapper;
    @Override
    public UserVo getMessage(TbUser tbUser) {
            UserVo userVo = userMapper.selectVo(tbUser.getId());
            //查出来的数据进行缩减，返回前端修改的数据
            List<MenuVo> menus = JsonUtil.sendJson(menuMapper.selectByRoleId(tbUser.getRoleId()));
            userVo.setMenus(menus);
            //设置token
            String token = TokenUtils.getToken(tbUser.getId().toString(), tbUser.getRoleId().toString(), tbUser.getPassword());
            userVo.setToken(token);
            return userVo;
    }
    @Override
    @Transactional
    public int addUser(UserDto userDto) {
        //进行密码加密
        userDto.setPassword(MD5Util.getMD5(userDto.getPassword()));
        TbUser user = new TbUser();
        //给user赋值插入user表
        BeanUtils.copyProperties(userDto,user);
        //根据部门名称获取部门id
        QueryWrapper<TbDept> deptWrapper = new QueryWrapper();
        deptWrapper.lambda().eq(TbDept::getName,userDto.getDeptName());
        user.setDeptId(deptMapper.selectOne(deptWrapper).getId());
        //根据角色名称获取角色id
        QueryWrapper<TbRole> roleWrapper = new QueryWrapper();
        roleWrapper.lambda().eq(TbRole::getName,userDto.getRoleName());
        int id = roleMapper.selectOne(roleWrapper).getId();
        user.setRoleId(roleMapper.selectOne(roleWrapper).getId());
        //给userDetail赋值插入userDetail表
        TbUserDetail userDetail = new TbUserDetail();
        BeanUtils.copyProperties(userDto,userDetail);
        if (userDto.getGender().equals("1")){
            userDetail.setGender(1);
        }else {
            userDetail.setGender(0);
        }
        int temp = userMapper.insert(user);
        int temp1 = userDetailMapper.insert(userDetail);
        if (temp > 0 && temp1 > 0){
            return user.getId();
        }
        throw new RuntimeException("插入异常，回滚");
    }

    @Override
    @Transactional
    public void updateUser(UserListVo userListVo) {
        TbUser user = new TbUser();
        //给user赋值插入user表
        user.setModifyTime(DateUtil.dateToLocalDateTime(new Date()));
        BeanUtils.copyProperties(userListVo,user);
        //根据部门名称获取部门id
        QueryWrapper<TbDept> deptWrapper = new QueryWrapper();
        deptWrapper.lambda().eq(TbDept::getName,userListVo.getDeptName());
        user.setDeptId(deptMapper.selectOne(deptWrapper).getId());
        //根据角色名称获取角色id
        QueryWrapper<TbRole> roleWrapper = new QueryWrapper();
        roleWrapper.lambda().eq(TbRole::getName,userListVo.getRoleName());
        user.setRoleId(roleMapper.selectOne(roleWrapper).getId());
        //给userDetail赋值插入userDetail表
        TbUserDetail userDetail = new TbUserDetail();
        BeanUtils.copyProperties(userListVo,userDetail);
        if (userListVo.getGender().equals("1")){
            userDetail.setGender(1);
        }else {
            userDetail.setGender(0);
        }
        int temp = userMapper.updateById(user);
        int temp1 = userDetailMapper.updateById(userDetail);
        if (temp > 0 && temp1 > 0){
            return ;
        }
        throw new RuntimeException("插入异常，回滚");
    }

    @Override
    @Transactional
    public void deleteUser(int userId, int modifyId) {
        Date date = new Date();
        TbUser user = userMapper.selectById(userId);
        user.setModifyId(modifyId);
        user.setModifyTime(DateUtil.dateToLocalDateTime(date));
        TbUserDetail userDetail = userDetailMapper.selectById(userId);
        userDetail.setModifyId(modifyId);
        userDetail.setModifyTime(DateUtil.dateToLocalDateTime(date));
//        queryWrapper.lambda().eq(TbUser::getId,userId);
        int temp = userMapper.updateById(user);
        int temp1 = userDetailMapper.updateById(userDetail);
        int temp2 = userMapper.deleteById(userId);
        int temp3 = userDetailMapper.deleteById(userId);
        if (temp > 0 && temp1 > 0 && temp2 > 0 && temp3 > 0){
            return ;
        }
        throw new RuntimeException("插入异常，回滚");
    }

    @Override
    public IPage<UserListVo> getList(Page<UserListVo> page,String userName,String phone) {
        return userMapper.getList(page,userName,phone);
    }

    @Override
    public int resetPwd(int userId, String password, int modifyId) {
        TbUser user = userMapper.selectById(userId);
        user.setPassword(password);
        user.setModifyId(modifyId);
        user.setModifyTime(DateUtil.dateToLocalDateTime(new Date()));
        QueryWrapper<TbUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbUser::getId,userId);
        return userMapper.update(user,queryWrapper);
    }

    @Override
    public List<UserEmailList> emailGetUserList() {
        return userMapper.emailGetUserList();
    }


}
